﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Product;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Product
{
    public class ItemTypeServices : IItemTypeServices
    {

        //异步方法
        public ItemTypeServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }


        /// <summary>
        /// 插入单条数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(ItemType model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = @"
                        with tab as (
                             select * from ItemType where Id = @Ids
                             union all 
                             select child.* from ItemType child join tab parent on parent.Id = child.ParentTypeId
                        )
                        delete from ItemType where Id in (select Id from tab)";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int Id)
        {
            // 删除一条数据
            string sql = @"
                        with tab as (
                             select * from ItemType where Id = @Id
                             union all 
                             select child.* from ItemType child join tab parent on parent.Id = child.ParentTypeId
                        )
                        delete from ItemType where Id in (select Id from tab)";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }


        /// <summary>
        /// 根据条件查询所有
        /// </summary>
        /// <param name="ItemOrProduct"></param>
        /// <param name="ItemTypeName"></param>
        /// <param name="EnableFlag"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ItemType>> GetAllAsync(string ItemOrProduct, string ItemTypeName, string EnableFlag)
        {
            string sql = "select * from ItemType where 1=1";

            // 判断条件是否为空，如果不为空则添加到查询语句中进行模糊查询
            if (!string.IsNullOrEmpty(ItemOrProduct))
            {
                sql += "and ItemOrProduct like @ItemOrProduct ";
            }
            if (!string.IsNullOrEmpty(ItemTypeName))
            {
                sql += "and ItemTypeName like @ItemTypeName ";
            }
            if (!string.IsNullOrEmpty(EnableFlag))
            {
                sql += "and EnableFlag like @EnableFlag ";
            }

            var list = await Db.QueryAsync<ItemType>(sql, new
            {
                ItemOrProduct = $"%{ItemOrProduct}%",
                ItemTypeName = $"%{ItemTypeName}%",
                EnableFlag = $"%{EnableFlag}%"
            });

            return list;
        }

        public async Task<IEnumerable<ItemType>> GetAsync(string ItemOrProduct, string EnableFlag, string ItemTypeName)
        {
            string sql = "select * from ItemType where EnableFlag='Y' ";

            if (!string.IsNullOrEmpty(ItemOrProduct))
            {
                sql += "and ItemOrProduct like @ItemOrProduct  ";
            }
            if (!string.IsNullOrEmpty(ItemTypeName))
            {
                sql += "and ItemTypeName like @ItemTypeName";
            }
            var list = await Db.QueryAsync<ItemType>(sql, new { ItemOrProduct = $"{ItemOrProduct}", ItemTypeName = $"{ItemTypeName}", EnableFlag = $"{EnableFlag}" });
            return list;
        }

        
        /// <summary>
        /// 事务修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(ItemType model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
